/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.zbxsoft.portal.tags  
 */
package com.dwusoft.platform.template;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.apache.tools.StrUtil;

/**
 * description:  标签模板显示标签
 * @author Hou Dayu 创建时间：2015-9-18  
 * 
 */
@SuppressWarnings("serial")
public class TemplateViewTag extends TagSupport {

	private Logger log = Logger.getLogger(TemplateViewTag.class);

	private String tempName;//模板名称
	private String valueName;//值名称
	private Object value;
	private String varNames;

	//private JspWriter writer;

	public int doStartTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
			Map<String, Object> map = new HashMap<String, Object>();
			if (StrUtil.isNotNull(valueName)) {
				map.put(valueName, request.getAttribute(valueName));
			} else if (StrUtil.isNotNull(varNames)) {
				String vars[] = varNames.split(",");
				for (int i = 0; i < vars.length; i++) {
					String var = vars[i];
					if ("loginUser".equals(var)) {
						map.put("loginUser", request.getSession().getAttribute("loginUser"));
					} else {
						if (StrUtil.isNotNull(vars[i])) {
							map.put(var, request.getAttribute(var));
						}
					}
				}
			}
			map.put("ctx", request.getContextPath());
			String html = FreemarkerHelper.instance().getTemplateStr(tempName, map);
			JspWriter writer = this.pageContext.getOut();// 返回一个JspWriter
			writer.print(html);
		} catch (Exception e) {
			log.error(e);
			return super.SKIP_BODY;
		}
		return super.EVAL_BODY_INCLUDE;
	}

	public String getTempName() {
		return tempName;
	}

	public void setTempName(String tempName) {
		this.tempName = tempName;
	}

	public String getValueName() {
		return valueName;
	}

	public void setValueName(String valueName) {
		this.valueName = valueName;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public String getVarNames() {
		return varNames;
	}

	public void setVarNames(String varNames) {
		this.varNames = varNames;
	}

}
